package us.mooresr.webtroopmanager.client.ui;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import us.mooresr.webtroopmanager.client.place.EventListPlace;
import us.mooresr.webtroopmanager.client.ui.EditPersonViewImpl.EditPersonUiBinder;
import us.mooresr.webtroopmanager.shared.Event;
import us.mooresr.webtroopmanager.shared.PersonSummary;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.SimpleCheckBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

public class EditEventViewImpl extends Composite implements EditEventView {
	private Presenter presenter;
	
	private Long eventId;
	
	private static EditEventUiBinder uiBinder = GWT.create(EditEventUiBinder.class);
	
	interface EditEventUiBinder extends UiBinder<Widget, EditEventViewImpl> {
		
	}
	
	// Used for the case where we know which index is selected and need to know what person that is
	private Map<Integer, PersonSummary> comboBoxMap;
	
	// Used for the case where we have a person's ID and need to select that person in the combo list
	private Map<Long, Integer> comboBoxReverseMap;
	
	@UiField 
	TextBox eventNameTextBox;
	
	@UiField
	TextBox departureLocationTextBox;
	
	@UiField
	TextBox destinationTextBox;
	
	@UiField
	DateBox departureDateBox;
	
	@UiField
	DateBox returnDateBox;
	
	@UiField
	SimpleCheckBox permitFiledCheckBox;
	
	@UiField
	SimpleCheckBox swimmingCheckBox;
	
	@UiField
	SimpleCheckBox boatingCheckBox;
	
	@UiField
	SimpleCheckBox climbingCheckBox;
	
	@UiField
	SimpleCheckBox flyingCheckBox;
	
	@UiField
	SimpleCheckBox highAdventureCheckBox;
	
	@UiField
	TextBox travelMilesTextBox;
	
	@UiField
	ListBox tourLeaderComboBox;
	
	@UiField
	ListBox asstLeaderComboBox;
	
	@UiHandler("okButton")
	void onOkClick(ClickEvent event) {
		Integer iTravelMiles;
		try {
			iTravelMiles = Integer.parseInt(travelMilesTextBox.getValue());
		} catch (Exception e) {
			iTravelMiles = 0;
		}
		
		Integer tourLeaderIndex = tourLeaderComboBox.getSelectedIndex();
		if (tourLeaderIndex == 0) {
			tourLeaderId = null;
		} else {
			PersonSummary tourLeader = comboBoxMap.get(tourLeaderIndex);
			if (tourLeader == null) {
				Window.alert("Oops");
			} else {
				tourLeaderId = tourLeader.getId();
			}
		}
		
		Integer asstLeaderIndex = asstLeaderComboBox.getSelectedIndex();
		if (asstLeaderIndex == 0) {
			asstLeaderId = null;
		} else {
			PersonSummary asstLeader = comboBoxMap.get(asstLeaderIndex);
			if (asstLeader == null) {
				// This shouldn't happen
				Window.alert("Oops");
			} else {
			    asstLeaderId = asstLeader.getId();
			}
		}
		
		Event e = new Event(eventId, eventNameTextBox.getValue(), departureLocationTextBox.getValue(),
				destinationTextBox.getValue(), departureDateBox.getValue(), returnDateBox.getValue(),
				permitFiledCheckBox.isChecked(), swimmingCheckBox.isChecked(), boatingCheckBox.isChecked(),
				climbingCheckBox.isChecked(), flyingCheckBox.isChecked(), highAdventureCheckBox.isChecked(),
				iTravelMiles, tourLeaderId, asstLeaderId);
		
		presenter.addEvent(e);
		presenter.goTo(new EventListPlace());	
	}
	
	@UiHandler("cancelButton")
	void onCancelClick(ClickEvent event) {
		presenter.goTo(new EventListPlace());
	}
	

	private Long tourLeaderId=null;
	private Long asstLeaderId=null;
	
	public EditEventViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		

		comboBoxMap = new HashMap<Integer, PersonSummary>();
		comboBoxReverseMap = new HashMap<Long, Integer>();
		
	}
	
	@Override
	public void setComboBoxes(List<PersonSummary> people) {
		
		comboBoxMap.clear();
		comboBoxReverseMap.clear();
		tourLeaderComboBox.clear();
		asstLeaderComboBox.clear();
		
		// Fill in the combo boxes for the leaders
		tourLeaderComboBox.insertItem("None", 0);
		asstLeaderComboBox.insertItem("None", 0);
		comboBoxMap.put(0, null);
		tourLeaderComboBox.setSelectedIndex(0);
		asstLeaderComboBox.setSelectedIndex(0);
		int i = 1;
		for (PersonSummary ps : people) {
			tourLeaderComboBox.insertItem(ps.toString(), i);
			if (ps.getId().equals(tourLeaderId)) {
				tourLeaderComboBox.setSelectedIndex(i);
			}
			
			asstLeaderComboBox.insertItem(ps.toString(), i);
			if (ps.getId().equals(asstLeaderId)) {
				asstLeaderComboBox.setSelectedIndex(i);
			}
			
			comboBoxMap.put(i, ps);			
			comboBoxReverseMap.put(ps.getId(), i);
			++i;
		}
	}

	@Override
	public void setData(Event event) {

		
		if (event == null) {
			eventId = null;
			eventNameTextBox.setValue("");
			departureLocationTextBox.setValue("");
			destinationTextBox.setValue("");
			departureDateBox.setValue(null);
			returnDateBox.setValue(null);
			permitFiledCheckBox.setChecked(false);
			swimmingCheckBox.setChecked(false);
			boatingCheckBox.setChecked(false);
			climbingCheckBox.setChecked(false);
			flyingCheckBox.setChecked(false);
			highAdventureCheckBox.setChecked(false);
			travelMilesTextBox.setValue("");
			tourLeaderId = null;
			asstLeaderId = null;
		} else {
			eventId = event.getId();
			eventNameTextBox.setValue(event.getEventName());
			departureLocationTextBox.setValue(event.getDepartureLocation());
			destinationTextBox.setValue(event.getDestination());
			departureDateBox.setValue(event.getDepartureDate());
			returnDateBox.setValue(event.getReturnDate());
			permitFiledCheckBox.setChecked(event.getPermitFiled());
			swimmingCheckBox.setChecked(event.getSwimming());
			boatingCheckBox.setChecked(event.getBoating());
			climbingCheckBox.setChecked(event.getClimbing());
			flyingCheckBox.setChecked(event.getFlying());
			highAdventureCheckBox.setChecked(event.getHighAdventure());
			travelMilesTextBox.setValue(event.getTravelMiles().toString());
			tourLeaderId = event.getTourLeader();
			asstLeaderId = event.getAsstTourLeader();
			
			// Select the correct entries for tour leader and assistant leader
			// Due to the asynchronous nature of the database calls the combo boxes
			// may not be filled in by the time we get the data.  So if we can't
			// find the indexes in the reverse map we just skip this part, they'll
			// get selected when the combo boxes get filled in
			
			
			Integer index;
			index = comboBoxReverseMap.get(tourLeaderId);
			if (index != null) {
				tourLeaderComboBox.setSelectedIndex(index);
			} else {
				// tourLeaderId is null, so we have no tour leader yet
				// if the combo box has been populated then "None" will always be index 0
				if (comboBoxReverseMap.size() != 0) {
					tourLeaderComboBox.setSelectedIndex(0);
				}
			}
			index = comboBoxReverseMap.get(asstLeaderId);
			if (index != null) {
				asstLeaderComboBox.setSelectedIndex(index);
			} else {
				// asstLeaderId is null, so we have no tour leader yet
				// if the combo box has been populated then "None" will always be index 0
				if (comboBoxReverseMap.size() != 0) {
					asstLeaderComboBox.setSelectedIndex(0);
				}
			}
			
		}
		

		
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
		
	}

}
